#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include<QLabel>
#include<QPushButton>
#include<string>
#include<iostream>
#include<QDebug>
#include<QKeyEvent>
#include <QPainter>
#include <stdlib.h>
#include <time.h>
#include<QMediaPlayer>
#include<QDialog>
#include<QMessageBox>
#include<QTimerEvent>
//全局
#define ZOOM 1

#define POINT 100

int X,Y;
int nowTime = 0,steps=0;
int timerId;
MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{

    ui->setupUi(this);
    //退出
    connect(ui->actiontuic,&QAction::triggered,[=](){
        this->close();
    });
    //新游戏
    connect(ui->actiond,&QAction::triggered,[=](){
        this->CreatNewGame();
    });
    //开发者信息
    connect(ui->action1_2,&QAction::triggered,[=]{
        QMessageBox::information(this,"开发者信息","开发者:2111792 张钧瑞");
    });
    //游戏说明
    connect(ui->action1,&QAction::triggered,[=](){
        QMessageBox::information(this,"游戏说明","按“↑”、“↓”、“←”、“→”对空白方块进行操作");
    });
    this->setFixedSize(480*ZOOM,640*ZOOM);
    this->setWindowIcon(QPixmap(":/game/MNLS.png"));
    this->setWindowTitle("拼图");
    //打印输出框


    init();

    //显示拼图
    this->update();
}
//初始化操作
void MainScene::init(){
    X=NUM-1;
    Y=NUM-1;
    //设置界面
    for(int i=0;i<NUM;i++){
        for(int j=0;j<NUM;j++){
            Game[i][j]=i*NUM+j;
        }
    }
    //设置步数和时间的字体
    QPalette pe;
    pe.setColor(QPalette::WindowText, Qt::white);
    ui->label->move(90,530);
    ui->label_2->move(340,530);
    ui->label->setPalette(pe);
    ui->label_2->setPalette(pe);


    CreatNewGame();
//    timerId=startTimer(1000);
}

void MainScene::CreatNewGame(){
    //打乱的拼图
    steps=0;
    ui->label_2->setText(QString::number(steps));
    srand((unsigned)time(NULL));
    //增加游戏打乱随机度
    for(int i=0;i<RANDTIME;i++){
           int r=rand() % NUM;
        if(r==0&&Y>0){
            GoUp();
        }else if(r==1&&Y<NUM-1){
            GoDown();
        }else if(r==2&&X>0){
            GoLeft();
        }else if(r==3&&X<NUM-1){
            GoRight();
        }

    }
    //开始计时
    killTimer(timerId);
    timerId=startTimer(1000);
    ui->label->setText("0");
    //开始声音
    StartSound();
    nowTime = 0;

    //游戏开始
}

//计时器
void MainScene::timerEvent(QTimerEvent *){

    ui->label->setText(QString::number(nowTime++));

}
void MainScene::paintEvent(QPaintEvent*)
{
    //背景打印
    QPainter painter (this);
    QPixmap pix,pix2;

    pix.load(":/game/BG.png");
    pix2.load(":/game/frame.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    painter.drawPixmap((this->width()-420)/2,(this->height()-420)/2,420,420,pix2);
    //时间
    QPixmap timeBox,stepBox;

    timeBox.load(":/game/textBox.png");
    stepBox.load(":/game/textBox.png");
    painter.drawPixmap(500,100,150,80,timeBox);
    painter.drawPixmap(500,300,150,80,stepBox);

    //步数

    //显示步数


    //显示游戏主体

    for(int i=0;i<NUM;i++)
    {
        for(int j=0;j<NUM;j++)
        {
            //计算当前图片名
            char nowPic[20]=":/game/block/";

            Game[i][j]>=10?nowPic[13]=Game[i][j]/10+'0',nowPic[14]=Game[i][j]%10+'0':nowPic[13]=Game[i][j]%10+'0';
            strcat(nowPic,".png");
            //qDebug()<<"PicName:"<<nowPic<<"  Number:"<<Game[i][j];

            QPixmap temPic;
            temPic.load(nowPic);
            painter.drawPixmap((this->width()-NUM*POINT)/2+j*POINT,(this->height()-NUM*POINT)/2+i*POINT,POINT,POINT,temPic);
        }
    }